package com.example.springbootdemo.config;


import com.example.springbootdemo.exception.MyException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author: ShengXiaJia
 * @Date: 2019/12/6 15:29
 * @Description: 捕获全局异常, 处理所有异常
 */
@RestControllerAdvice
public class ExceptionHandlerConfig {

    /**
     * 功能描述：处理自定义异常
     */
    @ExceptionHandler(value = MyException.class)
    Object MyException(MyException e, HttpServletRequest request) {
        //返回json数据，由前端去判断加载什么页面
//		Map<String, Object> map = new HashMap<>();
//		map.put("code", e.getCode());
//		map.put("msg", e.getMsg());
//		map.put("url", request.getRequestURL());
//		return map;

        // 进行页面跳转
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("error.html");
        modelAndView.addObject("msg", e.getMessage());
        return modelAndView;
    }

}
